﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Web.System.Services.Sites;
using SaaS.Model.Dto.Sites;
using Microsoft.AspNetCore.Mvc;
using XCore.Web;
using XCore.Authorization;
using SaaS.Core.Authorization;
using XCore.Extensions;
using XCore.Web.Models;
using Microsoft.AspNetCore.Authorization;

namespace Web.System.Controllers.Sites
{
    /// <summary>
    /// 产品
    /// </summary>
    [Route("api/site/[controller]/[action]")]
    [ApiController]
    public class ProjectController : SystemControllerBase
    {
        private readonly ProjectAppService _projectApplicationService;
        /// <summary>
        /// 构造
        /// </summary>
        public ProjectController(ProjectAppService projectApplicationService)
        {
            _projectApplicationService = projectApplicationService;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission(PermissionNames.Site_Project)]
        public ActionResult List(ProjectPageInput input)
        {
            var query = SDC.Projects.AsQueryable();
            query = query.Where(input.Name, c => c.Name == input.Name);
            return PageResult(query.OrderByDescending(c => c.CreationTime), c => new
            {
                c.CreationTime,
                c.Description,
                c.Id,
                c.IsRelease,
                c.Name,
                c.Sort
            });
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost, Permission(PermissionNames.Site_Project_Create)]
        public ActionResult Save(ProjectInput input)
        {
            return Ok(_projectApplicationService.CreateOrEdit(input));
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete, Permission(PermissionNames.Site_Project_Delete)]
        public ActionResult Delete(int id)
        {
            return Ok(_projectApplicationService.Delete(id));
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Permission(PermissionNames.Site_Project_Query)]
        public async Task<ActionResult<ProjectDto>> Info(int id)
        {
            return await _projectApplicationService.GetInfoAsync(id);
        }
        /// <summary>
        /// 发布产品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Permission(PermissionNames.Site_Project_Query)]
        public ActionResult Release(int id)
        {
            return Ok(_projectApplicationService.ReleaseAsync(id));
        }

        /// <summary>
        /// 产品授权
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Permission(PermissionNames.Site_Project_Authoriza)]
        public ActionResult Authoriza(ChildInput<string> input)
        {
            _projectApplicationService.Authoriza(input.Id, input.Values);
            return Ok();
        }
        /// <summary>
        /// 获取产品权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Permission(PermissionNames.Site_Project_Authoriza)]
        public ActionResult<List<string>> GetPermissions(int id)
        {
            return _projectApplicationService.GetPermissions(id).ToList();
        }
    }
}